﻿using System;
using System.Runtime.Serialization;
using System.Collections.Generic;
using COM.PreAlarm.Model;
using System.Data;
using System.Collections;
using System.Web;

namespace COM.PreAlarm.BLL
{
    public partial class Mdlist
    {
        partial void OnBLLCreated() { }

        public static DataTable GetListByType(MdlistInfo _MdlistInfo)
        {

            _MdlistInfo.TranType = "GetListByType";
            return GetDataTableByModel(_MdlistInfo);
        }

        public static DataTable GetDataTableByModel(MdlistInfo _MdlistInfo)
        {
            List<DBParameter> _listDBParameter = new List<DBParameter>();
            _listDBParameter.Add(new DBParameter("@TranType", _MdlistInfo.TranType));
            _listDBParameter.Add(new DBParameter("@UID", _MdlistInfo.UID));
            _listDBParameter.Add(new DBParameter("@type", _MdlistInfo.Type));
            _listDBParameter.Add(new DBParameter("@name", _MdlistInfo.Text));
            _listDBParameter.Add(new DBParameter("@County", _MdlistInfo.Value));
            _listDBParameter.Add(new DBParameter("@City", _MdlistInfo.Remark));
            Mdlist _Mdlist = new Mdlist();
            IDataReader sdr = _Mdlist.ExecuteReader("[dbo].[mdlist_sel]", _listDBParameter, System.Data.CommandType.StoredProcedure);
            DataTable dt = new DataTable();
            dt.Load(sdr);
            return dt;
        }

        //
        public static string GetTextByValue(string Value, string type)
        {
            string tempStr;
            Hashtable ht = new Hashtable();
            System.Web.Caching.Cache Cache = HttpRuntime.Cache;
            if (Cache[type] == null)
            {
                MdlistInfo _MdlistInfo = new MdlistInfo();
                _MdlistInfo.Type = type;
                DataTable dt = GetListByType(_MdlistInfo);
                foreach (DataRow dr in dt.Rows) {
                    ht.Add(dr["ID"].ToString(), dr["Name"].ToString());
                }
                Cache.Insert(type, ht, null, System.DateTime.Now.AddHours(8), System.Web.Caching.Cache.NoSlidingExpiration);
            }
            else
            {
                ht = (Hashtable)Cache[type];
            }
            try
            {
                tempStr = ht[Value].ToString();
            }
            catch (Exception e)
            {
                tempStr = string.Empty;
            }
            return tempStr;
        }

    }
}